package com.catersystem.zmy.catersystem.utils;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

/**
 * 语音合成工具类
 * Created by Administrator on 2017/11/3 0003.
 */

public class AudioUtils {
    private static final String TAG = "AudioUtils";
    private static AudioUtils audioUtils;
    private SpeechSynthesizer mTts;
    private int mNum;
    private int mCount;
    private String mContent;

    public AudioUtils() {
    }

    /**
     * 描述:单例
     */
    public static AudioUtils getInstance() {
        if (audioUtils == null) {
            synchronized (AudioUtils.class) {
                if (audioUtils == null) {
                    audioUtils = new AudioUtils();
                }
            }
        }
        return audioUtils;
    }

    private InitListener myInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
        }
    };


    /**
     * 描述:初始化语音配置
     */
    public void init(Context context) {
        mCount = SPUtils.getInstance().getString("voice_time") == "" ? 2 : Integer.parseInt(SPUtils.getInstance()
                .getString("voice_time"));
        //处理语音合成关键类
        mTts = SpeechSynthesizer.createSynthesizer(context, myInitListener);
        if (mTts != null) {
            mTts.setParameter(SpeechConstant.VOICE_NAME,"xiaoyan");//设置发音人
            mTts.setParameter(SpeechConstant.SPEED, "60");//设置语速
            mTts.setParameter(SpeechConstant.VOLUME, "100");//设置音量，范围0~100
            mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
        }
    }

    //设置发音人
    public void setVoicePeople(String name) {
        mTts.setParameter(SpeechConstant.VOICE_NAME, name);
    }

    /**
     * 描述:根据传入的文本转换音频并播放
     */
    public void speakText(String content) {
        mNum = 1;
        this.mContent = content;
        if (mTts != null)
            //开始合成
            mTts.startSpeaking(content, mSynListener);

    }

    /**
     * 设置播放次数
     *
     * @param count
     */
    public void setCount(int count) {
        this.mCount = count;
    }

    //合成监听器
    private SynthesizerListener mSynListener = new SynthesizerListener() {
        //会话结束回调接口，没有错误时，error为null
        public void onCompleted(SpeechError error) {
            if (mCount > mNum) {//循环播报次数
                mNum++;
                mTts.startSpeaking(mContent, mSynListener);
            }
        }

        //缓冲进度回调
        //percent为缓冲进度0~100，beginPos为缓冲音频在文本中开始位置，endPos表示缓冲音频在文本中结束位置，info为附加信息。
        public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
        }

        //开始播放
        public void onSpeakBegin() {
        }

        //暂停播放
        public void onSpeakPaused() {
        }

        //播放进度回调
        //percent为播放进度0~100,beginPos为播放音频在文本中开始位置，endPos表示播放音频在文本中结束位置.
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
        }

        //恢复播放回调接口
        public void onSpeakResumed() {
        }

        //会话事件回调接口
        public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
        }
    };

    /**
     * 销毁语音合成 防止内存溢出
     */
    public void onDestroy() {
        if (mTts != null) {
            mTts.stopSpeaking();
            mTts.destroy();
        }
    }
}
